/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package kaytto1;

import com.sun.lwuit.Graphics;
import com.sun.lwuit.Image;

/**
 *
 * @author Joteitti
 */
public class Utilities {

    public static Image createThumbail(Image source, int width, int height) {
        int sourceWidth = source.getWidth();
        int sourceHeight = source.getHeight();

        // Determine height
        if(height == -1)
            height = width * sourceHeight / sourceWidth;


        // Create temp image
        Image temp = Image.createImage(width, height);
        Graphics graphics = temp.getGraphics();

        // Draw thumbail image
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                // Set clipping
                graphics.setClip(x, y, 1, 1);

                // Calculate pixel to draw
                int dx = x * sourceWidth / width;
                int dy = y * sourceHeight / height;
                graphics.drawImage(source, x - dx, y - dy);
            }
        }

        return Image.createImage(temp.getRGB(), width, height);
    }

}
